;; Multiarray iterators

;; by Konrad Hinsen
;; last updated May 27, 2010

;; Copyright (c) Konrad Hinsen, 2010. All rights reserved.  The use
;; and distribution terms for this software are covered by the Eclipse
;; Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php)
;; which can be found in the file epl-v10.html at the root of this
;; distribution.  By using this software in any fashion, you are
;; agreeing to be bound by the terms of this license.  You must not
;; remove this notice, or any other, from this software.

(use 'nstools.ns)
(ns+ multiarray.seqs-and-iterators
  (:clone multiarray.utility))

(set! *warn-on-reflection* false)

;
; Iterator over first array dimension
;

(deftype MultiArrayIterator

  [#^clojure.lang.Atom index
   #^Integer           count
   #^clojure.lang.IFn  generator]

  java.util.Iterator
    (hasNext [this] (< @(. this index) (. this count)))
    (next [this]
      (if (.hasNext this)
	(let [index   (. this index)
	      element (generator @index)]
	  (swap! index inc)
	  element)
	(throw (java.util.NoSuchElementException.))))
    (remove [this] (throw (UnsupportedOperationException.))))


(defn make-dim0-iterator
  [index count generator]
  (new MultiArrayIterator (atom index) count generator))


(set! *warn-on-reflection* false)
